const bright_frag = /*wgsl*/ `
struct Input {  
    @location(0) uv : vec2<f32>,
};
struct Output {
    @location(0) color:vec4<f32>,
};
@group(0) @binding(0) var tDiffuse: texture_2d<f32>;
@group(0) @binding(1) var Sampler: sampler;
@group(0) @binding(2) var <uniform> luminosityThreshold : f32;
@group(0) @binding(3) var <uniform> smoothWidth : f32;
@fragment
fn main(input:Input) -> Output {
    var output:Output;
    var texel:vec4<f32>=textureSample(tDiffuse, Sampler,input.uv);
    var luma = vec3( 0.299, 0.587, 0.114 );
    var v = dot( texel.xyz, luma );
    var outputColor = vec4<f32>( 0,0,0,0 );
    var alpha = smoothstep( luminosityThreshold, luminosityThreshold + smoothWidth, v );
    output.color = mix( outputColor, texel,alpha );
    // output.color = vec4(1.0,1.0,0.0,1.0);
    return output;
}
`;

const getBlurFragment = (kernelRadius) => {
    const blur_frag = /*wgsl*/ `
        @group(0) @binding(0) var colorTexture: texture_2d<f32>;
        @group(0) @binding(1) var texSampler: sampler;
        @group(0) @binding(2) var <uniform> invSize :vec2<f32>;
        @group(0) @binding(3) var <uniform> direction :vec2<f32>;
        @group(0) @binding(4) var <storage> gaussianCoefficients: array<f32>;  // 改为 storage
        
        @fragment
        fn main(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
            var weightSum = gaussianCoefficients[0];
            var diffuseSum = textureSample( colorTexture,texSampler, uv ).rgb * weightSum;
            for( var i:i32 = 1; i < ${kernelRadius}; i ++ ) {
                var x = f32(i);
                var w = gaussianCoefficients[i];
                var uvOffset = direction * invSize * x;
                var sample1 = textureSample( colorTexture,texSampler, uv + uvOffset ).rgb;
                var sample2 = textureSample( colorTexture,texSampler, uv - uvOffset ).rgb;
                diffuseSum += (sample1 + sample2) * w;
                weightSum += 2.0 * w;
            }
            var fragColor = vec4(diffuseSum/weightSum, 1.0);
            return fragColor;
        }`;
    return blur_frag;
}

const merge_frag = /*wgsl*/ `
@group(0) @binding(0) var originalTex: texture_2d<f32>;
@group(0) @binding(1) var brightTex: texture_2d<f32>;
@group(0) @binding(2) var texSampler: sampler;

@fragment
fn main(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
    let original = textureSample(originalTex, texSampler, uv);
    let bright = textureSample(brightTex, texSampler, uv);
    return original+vec4( 1.0 )*bright ; // 简单相加，您可以根据需要调整混合方式
    // return original;
}`
const composite_frag = /*wgsl*/ `
@group(0) @binding(0) var blurTexture1: texture_2d<f32>;
@group(0) @binding(1) var blurTexture2: texture_2d<f32>;
@group(0) @binding(2) var blurTexture3: texture_2d<f32>;
@group(0) @binding(3) var blurTexture4: texture_2d<f32>;
@group(0) @binding(4) var blurTexture5: texture_2d<f32>;
@group(0) @binding(5) var texSampler: sampler;
@group(0) @binding(6) var<uniform> bloomStrength: f32;
@group(0) @binding(7) var<uniform> bloomRadius: f32;
@group(0) @binding(8) var<storage> bloomFactors: array<f32>;  // 改为 storage
@group(0) @binding(9) var<storage> bloomTintColors: array<vec3<f32>>;  // 改为 storage

fn lerpBloomFactor(factor: f32) -> f32 {
    let mirrorFactor = 1.2 - factor;
    return mix(factor, mirrorFactor, bloomRadius);
}

@fragment
fn main(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
    let color1 = textureSample(blurTexture1, texSampler, uv) * vec4<f32>(bloomTintColors[0], 1.0) * lerpBloomFactor(bloomFactors[0]);
    let color2 = textureSample(blurTexture2, texSampler, uv) * vec4<f32>(bloomTintColors[1], 1.0) * lerpBloomFactor(bloomFactors[1]);
    let color3 = textureSample(blurTexture3, texSampler, uv) * vec4<f32>(bloomTintColors[2], 1.0) * lerpBloomFactor(bloomFactors[2]);
    let color4 = textureSample(blurTexture4, texSampler, uv) * vec4<f32>(bloomTintColors[3], 1.0) * lerpBloomFactor(bloomFactors[3]);
    let color5 = textureSample(blurTexture5, texSampler, uv) * vec4<f32>(bloomTintColors[4], 1.0) * lerpBloomFactor(bloomFactors[4]);
    
    return bloomStrength * (color1 + color2 + color3 + color4 + color5);
}`
const blend_frag = /*wgsl*/ `
@group(0) @binding(0) var tDiffuse: texture_2d<f32>;
@group(0) @binding(1) var texSampler: sampler;
@group(0) @binding(2) var<uniform> opacity: f32;

@fragment
fn main(@location(0) uv: vec2<f32>) -> @location(0) vec4<f32> {
    var texel =textureSample(tDiffuse, texSampler, uv);
    return opacity * texel;
}`
export { bright_frag, merge_frag, getBlurFragment, composite_frag, blend_frag };
